//删除数据
function deletelist(obj,url,post){
	var prop=layer.open({
		type: 1,
		title: false,
		id: "delete" //防止重复弹出
			,
		content: "<div class='am-vertical-align-middle' style='width:100%;text-align:center'>是否删除</div>",
		btn: ['确定', '取消'],
		btnAlign: 'c' //按钮居中
			,
		shade: 0 //不显示遮罩
			,
		yes: function() {
			console.log(obj.closest('tr').data('id'))
			var data = {
				id: obj.closest('tr').data('id')
			};
			var deleteload = layer.load(1, {
				shade: [0.1, '#fff'] //0.1透明度的白色背景
			});
			$.ajax({
				method: "POST",
				url:url,
				data: data,
				success: function(res) {
					if(res.code == 1) {
						layer.close(deleteload);
//										$(that).closest('tr').remove();
						var Load = layer.load(1, {
							shade: [0.1, '#fff'] //0.1透明度的白色背景
						});
						post(Load)
						layer.close(prop);
						
					} else if(res.code == 0) {
						layer.close(prop);
						Err(res.msg ? res.msg : '操作失败，稍候再试');
					}

				}
			});

		},
		btn2: function() {
			layer.close(prop);
		}
	});
}
//数据筛选
function filtrate(res, keyarr, mainarr) {
	var arr = res;
	for(var i = 0; i < keyarr.length; i++) {
		if(mainarr[i] == "") {

		} else {
			if(mainarr[i]==0){}else{
				for(var j = 0; j < arr.length; j++) {
					if(keyarr[i].match('id')){
						if(arr[j][keyarr[i]]) {
							var str = new RegExp(mainarr[i], "i")
							var mainval=arr[j][keyarr[i]]
							if(mainval==mainarr[i]) {
							} else {
								arr.splice(j, 1);
								j--;
							}
						} else {
							arr.splice(j, 1);
							j--;
						}
					}else{
						if(arr[j][keyarr[i]]) {
							var str = new RegExp(mainarr[i], "i")
							var mainval=arr[j][keyarr[i]]+""
							if(mainval.match(str)) {
							} else {
								arr.splice(j, 1);
								j--;
							}
						} else {
							arr.splice(j, 1);
							j--;
						}
					}
				}
			}
		}
	}
	return arr
}
//根据时间进行数据筛选
function filtratedate(res, keyarr, datearr) {
	var arr = res;
	var date1 = (new Date(datearr[0]).getTime() - 0);
	console.log(date1)
	var date2 = (new Date(datearr[1]).getTime() - 0)
	if(datearr[0] == "") {} else {
		for(var j = 0; j < arr.length; j++) {
			if(arr[j][keyarr[0]]) {
				var time1 = (new Date(arr[j][keyarr[0]]).getTime() - 0);
				if(time1 > date1) {

				} else {
					arr.splice(j, 1)
					j--
				}
			} else {
				arr.splice(j, 1)
				j--
			}
		}
	}
	if(datearr[1] == "") {} else {
		
		for(var j = 0; j < arr.length; j++) {
			if(arr[j][keyarr[1]]) {
				var time2 = (new Date(arr[j][keyarr[1]]).getTime() - 0);
				console.log(time2)
				if(time2 < date2) {

				} else {
					arr.splice(j, 1)
					j--
				}
			} else {
				arr.splice(j, 1)
				j--
			}
		}
	}

	return arr
}
//添加内容
//res 请求回来的数据    page页数   tddata数组，里面是td的数据的key  addoperation操作部分   idkey在tr上的data-id的key
function tbodymainf(res, page, tddata, addoperation, idkey) {
	var tbodymain = ''
	if(res.length == 0) {
		tbodymain = `<tr ><td>无数据</td></tr>`
	} else {
		if((page * 10) > res.length) {
			for(var tridx = (page * 10 - 10); tridx < res.length; tridx++) {
				tbodymain += `<tr data-id='${res[tridx][idkey]}'>`
				tbodymain = circulation(res[tridx], tddata, tbodymain)
				tbodymain += addoperation
				tbodymain += `</tr>`
			};
		} else {
			for(var tridx = (page * 10 - 10); tridx < (page * 10); tridx++) {
				tbodymain += `<tr data-id='${res[tridx][idkey]}'>`
				tbodymain = circulation(res[tridx], tddata, tbodymain)
				tbodymain += addoperation
				tbodymain += `</tr>`
			};
		}
	}
	return tbodymain
};
//操作按钮有变化
function tbodymainf1(res, page, tddata, addoperation, idkey) {
	console.log(1)
	var tbodymain = ''
	if(res.length == 0) {
		tbodymain = `<tr ><td>无数据</td></tr>`
	} else {
		if((page * 10) > res.length) {
			for(var tridx = (page * 10 - 10); tridx < res.length; tridx++) {
				
				tbodymain += `<tr data-id='${res[tridx][idkey]}'>`
				tbodymain = circulation(res[tridx], tddata, tbodymain)
				if(res[tridx].state==1){
				tbodymain += addoperation[0]
				}else{
				tbodymain += addoperation[1]
				}
				tbodymain += `</tr>`
			};
		} else {
			for(var tridx = (page * 10 - 10); tridx < (page * 10); tridx++) {
				tbodymain += `<tr data-id='${res[tridx][idkey]}'>`
				tbodymain = circulation(res[tridx], tddata, tbodymain)
				tbodymain += addoperation
				tbodymain += `</tr>`
			};
		}
	}
	return tbodymain
};

//data为每个td 的key值
function circulation(obj, data, tbodymain) {
	for(var tdidx = 0; tdidx < data.length; tdidx++) {
		if(obj[data[tdidx]]) {

			if(data[tdidx] == 'departure' || data[tdidx] == "destination" || data[tdidx] == "people") {
				tbodymain += `<td class='td-address-max'>${obj[data[tdidx]]}</td>`
			} else {
				tbodymain += `<td>${obj[data[tdidx]]}</td>`
			}

		} else {
			tbodymain += `<td>暂无信息</td>`
		}

	}
	return tbodymain
}

//页码变换

function pagechang(count, pagenum, page) {
	var pagelist = '';
	var lastpage = Math.ceil(count / pagenum)
	if(count > pagenum) {
		if(page > 6) {
			if(page > lastpage - 6) {
				pagelist = `<li>
							<a class='laquo'>&laquo;</a>
						</li>`
				pagelist += `<li>
								<a class='pageli'>1</a>
							</li>`
				pagelist += `<li>
								<a class='pageli'>2</a>
							</li>`
				pagelist += `<li class="disabled"><span>...</span></li>`
				for(var i = lastpage - 8; i < lastpage + 1; i++) {
					if(i == page) {
						pagelist += `<li class="active"><span>${i}</span></li>`
					} else {
						pagelist += `<li>
									<a class='pageli'>${i}</a>
								</li>`
					}
				}
				if(page == lastpage) {
					pagelist += `<li class="disabled"><span>&laquo;</span></li>`
				} else {
					pagelist += `<li>
								<a class='raquo'>&raquo;</a>
							</li>`
				};

			} else {
				pagelist = `<li>
							<a class='laquo'>&laquo;</a>
						</li>`
				pagelist += `<li>
								<a class='pageli'>1</a>
							</li>`
				pagelist += `<li>
								<a class='pageli'>2</a>
							</li>`
				pagelist += `<li class="disabled"><span>...</span></li>`

				for(var i = (page - 3); i < (page - 0 + 4); i++) {
					if(i == page) {
						pagelist += `<li class="active"><span>${i}</span></li>`
					} else {
						pagelist += `<li>
									<a class='pageli'>${i}</a>
								</li>`
					}
				}
				pagelist += `<li class="disabled"><span>...</span></li>`
				pagelist += `<li>
								<a class='pageli'>${lastpage-1}</a>
							</li>`
				pagelist += `<li>
					<a class='pageli'>${lastpage}</a>
				</li>`
				pagelist += `<li>
								<a class='raquo'>&raquo;</a>
							</li>`

			}
		} else {
			if(page == 1 || !page) {
				pagelist = `<li class="disabled"><span>&laquo;</span></li>`
			} else {
				pagelist = `<li>
							<a class='laquo'>&laquo;</a>
						</li>`
			};
			if(lastpage <= 10) {
				for(var i = 0; i < lastpage; i++) {
					if((i + 1) == page) {
						pagelist += `<li class="active"><span>${i+1}</span></li>`
					} else {
						pagelist += `<li>
										<a class='pageli'>${i+1}</a>
									</li>`
					}
				}
			} else {
				for(var i = 0; i < 8; i++) {
					if((i + 1) == page) {
						pagelist += `<li class="active"><span>${i+1}</span></li>`
					} else {
						pagelist += `<li>
									<a class='pageli'>${i+1}</a>
								</li>`
					}
				}
				pagelist += `<li class="disabled"><span>...</span></li>`
				pagelist += `<li>
							<a class='pageli'>${lastpage-1}</a>
						</li>`
				pagelist += `<li>
				<a class='pageli'>${lastpage}</a>
			</li>`
			}

			if(page == lastpage) {
				pagelist += `<li class="disabled"><span>&raquo;</span></li>`
			} else {
				pagelist += `<li>
							<a class='raquo'>&raquo;</a>
						</li>`
			};
		}
		return pagelist

	}
}

//购车申请
function pagechang1(count, pagenum, page) {
	var pagelist = '';
	var lastpage = Math.ceil(count / pagenum)
	if(count > pagenum) {
		if(page > 6) {
			if(page > lastpage - 6) {
				pagelist = `<li>
							<a class='laquo'>&laquo;</a>
						</li>`
				pagelist += `<li>
								<a class='pageli'>1</a>
							</li>`
				pagelist += `<li>
								<a class='pageli'>2</a>
							</li>`
				pagelist += `<li class="disabled"><span>...</span></li>`
				for(var i = lastpage - 8; i < lastpage + 1; i++) {
					if(i == page) {
						pagelist += `<li class="active"><span>${i}</span></li>`
					} else {
						pagelist += `<li>
									<a class='pageli'>${i}</a>
								</li>`
					}
				}
				if(page == lastpage) {
					pagelist += `<li class="disabled"><span>&laquo;</span></li>`
				} else {
					pagelist += `<li>
								<a class='raquo'>&raquo;</a>
							</li>`
				};

			} else {
				pagelist = `<li>
							<a class='laquo'>&laquo;</a>
						</li>`
				pagelist += `<li>
								<a class='pageli'>1</a>
							</li>`
				pagelist += `<li>
								<a class='pageli'>2</a>
							</li>`
				pagelist += `<li class="disabled"><span>...</span></li>`

				for(var i = (page - 3); i < (page - 0 + 4); i++) {
					if(i == page) {
						pagelist += `<li class="active"><span>${i}</span></li>`
					} else {
						pagelist += `<li>
									<a class='pageli'>${i}</a>
								</li>`
					}
				}
				pagelist += `<li class="disabled"><span>...</span></li>`
				pagelist += `<li>
								<a class='pageli'>${lastpage-1}</a>
							</li>`
				pagelist += `<li>
					<a class='pageli'>${lastpage}</a>
				</li>`
				pagelist += `<li>
								<a class='raquo'>&raquo;</a>
							</li>`

			}
		} else {
			if(page == 1 || !page) {
				pagelist = `<li class="disabled"><span>&laquo;</span></li>`
			} else {
				pagelist = `<li>
							<a class='laquo'>&laquo;</a>
						</li>`
			};
			if(lastpage <= 10) {
				for(var i = 0; i < lastpage; i++) {
					if((i + 1) == page) {
						pagelist += `<li class="active"><span>${i+1}</span></li>`
					} else {
						pagelist += `<li>
										<a class='pageli'>${i+1}</a>
									</li>`
					}
				}
			} else {
				for(var i = 0; i < 8; i++) {
					if((i + 1) == page) {
						pagelist += `<li class="active"><span>${i+1}</span></li>`
					} else {
						pagelist += `<li>
									<a class='pageli'>${i+1}</a>
								</li>`
					}
				}
				pagelist += `<li class="disabled"><span>...</span></li>`
				pagelist += `<li>
							<a class='pageli'>${lastpage-1}</a>
						</li>`
				pagelist += `<li>
				<a class='pageli'>${lastpage}</a>
			</li>`
			}

			if(page == lastpage) {
				pagelist += `<li class="disabled"><span>&raquo;</span></li>`
			} else {
				pagelist += `<li>
							<a class='raquo'>&raquo;</a>
						</li>`
			};
		}
		return pagelist

	}
}
//排序
//arr 需排列的数组
//key 根据该key进行排序
//type 排序类型   1排序数字   2排序汉字（根据首字母，需先引用Convert_Pinyin.js）
//sort 升降序  1升序  2降序
function rank(arr,key,type,sort){
	var arr1=[];
	var arr2=[];
	if(type==1){
		for(var i=0;i<arr.length;i++){
			arr1.push(arr[i][key])
		}
		if(sort==1){
			arr1.sort()
		}else if(sort==2){
			arr1.sort()
			arr1.reverse()
		}
		for(var i1=0;i1<arr.length;i1++){
			for(var i2=0;i2<arr1.length;i2++){
				if(arr[i1][key]==arr1[i2]){
					arr2.push(arr[i1]);
				}
			}
		}
		
	}else if(type==2){
		var letter=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
		var arr1=arr
		for(var i=0;i<arr1.length;i++){
			arr1[i].letter=pinyin.getCamelChars(arr1[i][key])
		}
		console.log(arr1)
		for(var i2=0;i2<letter.length;i2++){
			for(var i1=0;i1<arr1.length;i1++){
				console.log(arr1[i1].letter[0]==letter[i2])
				if(arr1[i1].letter[0]==letter[i2]){
					arr2.push(arr1[i1])
				}
			}
		}
	}else if(type==3){
		
	}
	return arr2
}

//修改之后的

//添加内容
//res 请求回来的数据    page页数   tddata数组，里面是td的数据的key  addoperation操作部分   idkey在tr上的data-id的key
function tbodymainf2(res, page, tddata, addoperation, idkey) {
	var tbodymain = ''
	console.log(res)
	if(res.length == 0) {
		tbodymain = `<tr ><td>无数据</td></tr>`
	} else {
		if((page * 10) > res.length) {
			for(var tridx = (page * 10 - 10); tridx < res.length; tridx++) {
				tbodymain += `<tr data-id='${res[tridx][idkey]}'>`
				tbodymain = circulation2(res[tridx], tddata, tbodymain)
				tbodymain += addoperation
				tbodymain += `</tr>`
			};
		} else {
			for(var tridx = (page * 10 - 10); tridx < (page * 10); tridx++) {
				tbodymain += `<tr data-id='${res[tridx][idkey]}'>`
				tbodymain = circulation2(res[tridx], tddata, tbodymain)
				tbodymain += addoperation
				tbodymain += `</tr>`
			};
		}
	}
	return tbodymain
};


//data为每个td 的key值
//type  1 直接赋值   2是a标签，带跳转  3是正负值判断  4是根据状态进行字体颜色区分
function circulation2(obj, data, tbodymain) {
	for(var tdidx = 0; tdidx < data.length; tdidx++) {
		if(obj[data[tdidx].key]) {
			if(data[tdidx].type==1){
				if(data[tdidx].class){
					tbodymain += `<td class='${data[tdidx].class}'>${obj[data[tdidx].key]}</td>`
				}else{
					tbodymain += `<td>${obj[data[tdidx].key]}</td>`
				}
			}else if(data[tdidx].type==2){
				var path='#';
				if(data[tdidx].url){
					path=data[tdidx].url+'?car_id='+data[tdidx].id
				}
				if(data[tdidx].class){
					tbodymain += `<td class='${data[tdidx].class}'><a href="${path}">${obj[data[tdidx].key]}</a></td>`
				}else{
					tbodymain += `<td><a href="${path}">${obj[data[tdidx].key]}</a></td>`
				}
			}else if(data[tdidx].type==3){
				if(obj[data[tdidx].state]==1){
					if(data[tdidx].class){
						tbodymain += `<td class='${data[tdidx].class}'>-${obj[data[tdidx].key]}</td>`
					}else{
						tbodymain += `<td>-${obj[data[tdidx].key]}</td>`
					}
				}else{
					if(data[tdidx].class){
						tbodymain += `<td class='${data[tdidx].class}'>+${obj[data[tdidx].key]}</td>`
					}else{
						tbodymain += `<td>+${obj[data[tdidx].key]}</td>`
					}
				}
			}else if(data[tdidx].type==4){
				if(data[tdidx].class){
					if(data[tdidx].color){
						if(obj[data[tdidx].color]==1){
							tbodymain += `<td class='${data[tdidx].class}'>${obj[data[tdidx].key]}</td>`
						}else{
							tbodymain += `<td class='${data[tdidx].class}' style='color:red'>${obj[data[tdidx].key]}</td>`
						}
					}
				}else{
					if(data[tdidx].color){
						if(obj[data[tdidx].color]==1){
							tbodymain += `<td>${obj[data[tdidx].key]}</td>`
						}else{
							tbodymain += `<td style='color:red'>${obj[data[tdidx].key]}</td>`
						}
					}
				}
			}
			

		} else {
			tbodymain += `<td>暂无信息</td>`
		}

	}
	return tbodymain
}
